package com.viphuli.fragment;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import butterknife.Bind;
import butterknife.OnClick;

import com.loopj.android.http.RequestParams;
import com.viphuli.adapter.OrderAdapter;
import com.viphuli.app.R;
import com.viphuli.common.AccessTokenKeeper;
import com.viphuli.common.MyPageHelper;
import com.viphuli.http.ApiRequest;
import com.viphuli.http.bean.part.PurchaseOrder;
import com.viphuli.http.handler.MyBaseHttpResponseHandler.HandlerType;

public class PurchaseOrderFragment extends BaseProgressFragment implements OnItemClickListener {

	@Bind(R.id.layout_order_list_empty)
	protected ViewGroup emptyLayout;

	@Bind(R.id.layout_order_list_listview)
	protected ListView listView;

	private BaseAdapter adapter;

	private List<PurchaseOrder> list = new ArrayList<PurchaseOrder>();

	private PurchaseOrder curOrder;

	@Override
	protected int obtainContentRes() {
		return R.layout.frag_order;
	}

	@Override
	protected void initParams() {

	}

	@Override
	protected void initView() {

		//标题
		caller.setTitle(getString(R.string.str_main_tab_name_purchase));

		listView.setOnItemClickListener(this);

	}

	@Override
	protected void initData() {
		clean();
		initRequest(HandlerType.loading);
	}

	@Override
	public void onBackPressed() {
	}

	@OnClick(R.id.btn_order)
	public void order() {
		MyPageHelper.main.showMyPage(caller);
	}

	@Override
	protected void initRequest(HandlerType handlerType) {

		//下拉刷新操作恢复初始状态
		if (HandlerType.refresh.equals(handlerType)) {
			clean();
		}

		//网络请求
		AccessTokenKeeper token = AccessTokenKeeper.readAccessToken(getActivity());

		if (token.isLogin()) {

			RequestParams params = new RequestParams();
			params.put("uid", token.getOpenId());

			if (HandlerType.more.equals(handlerType)) {
				params.put("page", page + 1);
			} else {
				params.put("page", page);
			}

			ApiRequest.purchaseOrder.request(this, params, handlerType);

		} else {
			doShowLogin();
		}
	}

	public ViewGroup getEmptyLayout() {
		return emptyLayout;
	}

	public ListView getListView() {
		return listView;
	}

	public List<PurchaseOrder> getList() {
		return list;
	}

	public void setAdapter(OrderAdapter adapter) {
		this.adapter = adapter;
	}

	public BaseAdapter getBaseAdapter() {
		return adapter;
	}

	public void setBaseAdapter(BaseAdapter adapter) {
		this.adapter = adapter;
	}

	public PurchaseOrder getCurOrder() {
		return curOrder;
	}

	public void setCurOrder(PurchaseOrder curOrder) {
		this.curOrder = curOrder;
	}

	public void setPage(int page) {
		this.page = page;
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

		PurchaseOrder order = list.get(position);

		Bundle args = new Bundle();
		args.putString("order_id", order.getOrderId());
		MyPageHelper.purchaseOrderInfo.showMyPage(getActivity(), args);
	}

	private void clean() {
		list.clear();
		page = 1;
	}

}
